package com.vrv.liteflow.controller;

import com.alibaba.fastjson.JSONObject;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Bang
 * @ClassName: TestController
 * @Desc: TODO
 * @Date: 2023/11/01 15:54
 * @Version: 1.0
 * @Modified By:
 */
@RestController
public class TestController  implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/test")
    public Object testConfig(String chain, String param) {
        // 传参 param:流程入参 JSONObject.class:上下文对象
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chain, param, JSONObject.class);
        JSONObject contextBean = liteflowResponse.getContextBean(JSONObject.class);
        return contextBean;
    }

    @GetMapping("/test2")
    public Object testConfig2(String chain, String param) {
        JSONObject context = new JSONObject();
        context.put("init", "init");
        // 传参 param:流程入参 context:上下文对象
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(chain, param, context);
        JSONObject contextBean = liteflowResponse.getContextBean(JSONObject.class);
        return contextBean;
    }

    public String sayHello(String msg) {
        System.out.println("sayHello " + msg);
        return "sayHello " + msg;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
